package cn.wolfcode.crm.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.service.IRoleService;

@Controller
@RequestMapping("/role")
public class RoleController {
	@Autowired
	private IRoleService roleService;
	
	@Autowired
	private IPermissionService permissionService;
	
	@RequestMapping("/list")
	public String list(@ModelAttribute("qo") QueryObject qo, Model model) {
		PageInfo<Role> pageInfo = roleService.list(qo);
		
		model.addAttribute("pageInfo", pageInfo);
		return "role/list";
	}
	
	@RequestMapping("/input")
	public String input(Long id, Model model) {
		if (id != null) {
			model.addAttribute("entity", roleService.get(id));
		}
		model.addAttribute("permissions", permissionService.listNotPage(null));
		return "role/input";
	}

	@RequestMapping("/saveOrUpdate")
	public String saveOrUpdate(Role entity) {
		roleService.saveOrUpdate(entity);
		return "redirect:/role/list.do";
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Object delete(Long id) {
		String message = "";
		if (id != null) {
			message = roleService.delete(id);
		}
		return message;
	}
	

}
